﻿using System;
using KZ.Utilities;
using System.Reflection;
using System.Data;

namespace KZ.Express.H.ENT
{
    [Serializable]
    public class BaseENT
    {
        /// <summary>
        /// 0: start
        /// 1: recovery
        /// 2: no-recovery
        /// </summary>
        public virtual int IsRecovery { get; set; }

        [NotInsertUpdate]
        public virtual string Table { get { return String.Empty; } }

        [NotInsertUpdate]
        public virtual string Name { get { return String.Empty; } }

        [NotInsertUpdate]
        public virtual string PrimaryKey { get { return String.Empty; } }

        [NotInsertUpdate]
        public virtual string PrimaryDescription { get { return String.Empty; } }

        [NotInsertUpdate]
        public virtual string InsertProcedure { get { return String.Empty; } }

        [NotInsertUpdate]
        public virtual string UpdateProcedure { get { return String.Empty; } }

        [NotInsertUpdate]
        public virtual string DeleteProcedure { get { return "proc_Common_Delete"; } }

        [NotInsertUpdate]
        public virtual string SelectProcedure { get { return String.Empty; } }

        public virtual void ValidateData()
        {
            string errMsg = String.Empty;

            PropertyInfo[] properties = this.GetType().GetProperties();
            foreach (PropertyInfo property in properties)
            {
                object[] obj = property.GetCustomAttributes(typeof(RequiredAttribute), true);
                if (obj.Length > 0)
                {
                    RequiredAttribute attr = obj[0] as RequiredAttribute;
                    if (property.PropertyType == typeof(int) || property.PropertyType == typeof(int?) ||
                        property.PropertyType == typeof(decimal) || property.PropertyType == typeof(decimal?) ||
                        property.PropertyType == typeof(DateTime?))
                    {
                        if (property.GetValue(this, null) == null)
                            errMsg += attr.ErrorMessage + Environment.NewLine;
                    }
                    else if (property.PropertyType == typeof(string))
                    {
                        if (String.IsNullOrEmpty(Utility.StringParse(property.GetValue(this, null)).Trim()))
                            errMsg += attr.ErrorMessage + Environment.NewLine;
                    }
                }
                obj = property.GetCustomAttributes(typeof(StringLengthAttribute), true);
                if (obj.Length > 0)
                {
                    StringLengthAttribute attr = obj[0] as StringLengthAttribute;
                    string value = Utility.StringParse(property.GetValue(this, null));
                    if (value.Length < attr.Min || value.Length > attr.Max)
                        errMsg += attr.ErrorMessage + Environment.NewLine;
                }
            }

            if (!String.IsNullOrEmpty(errMsg.Trim()))
                throw new BusinessLogicException(errMsg);
        }
    }
}
